<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务id" prop="jobId">
        <el-select v-model="queryParams.jobId" placeholder="请选择任务" clearable>
          <el-option
            v-for="job in jobList"
            :key="job.jobId"
            :label="job.jobName"
            :value="job.jobId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable>
          <el-option
            v-for="dict in dict.type.data_sync_review_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="发起人" prop="Initiator">
        <el-input
          v-model="queryParams.Initiator"
          placeholder="请输入发起人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="reviewer">
        <el-input
          v-model="queryParams.reviewer"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="审核id" align="center" prop="id" />
      <el-table-column label="任务名称" align="center" prop="jobName" />
      <el-table-column label="任务状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.data_sync_review_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="发起人" align="center" prop="initiator" />
      <el-table-column label="审核人" align="center" prop="reviewer" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['data:review:edit']"
          >审核</el-button>
          <el-button
            v-if="scope.row.status !== '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleQuery(scope.row)"
            v-hasPermi="['data:review:edit']"
          >查看批注</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审核弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.data_sync_review_status"
              :key="dict.value"
              :label="dict.value"
              :disabled="!update"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="remark">
          <el-input 
          v-model="form.remark" 
          placeholder="请输入审核备注" 
          type="textarea"
          :rows="5"
          :disabled="!update"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button 
        type="primary" 
        @click="submitForm" 
        v-if="update"
        >提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReview, getReview, updateReview } from "@/api/data/review";
import { listJob} from "@/api/data/job";
import { listUser } from "@/api/system/user";

export default {
  name: "Review",
  dicts: ['data_sync_review_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // review表格数据
      reviewList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobId: null,
        status: null,
        Initiator: null,
        reviewer: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "任务状态不能为空", trigger: "change" }
        ],
      },
      // 用户信息查询列表
      userList: [],
      // 数据同步任务查询列表
      jobList: [],
      // 是否为审阅操作
      update: false
    };
  },
  created() {
    this.getList();
    // this.fetchUserList();
    this.fetchJobList();
  },
  methods: {
    /** 查询review列表 */
    getList() {
      this.loading = true;
      listReview(this.queryParams).then(response => {
        this.reviewList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        jobId: null,
        status: null,
        Initiator: null,
        reviewer: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 审核按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReview(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "任务审核";
        this.update = true;
      });
    },
    /** 审核按钮操作 */
    handleQuery(row) {
      this.reset();
      const id = row.id || this.ids
      getReview(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "任务审核";
        this.update = false;
      });
    },
    /** 审核按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateReview(this.form).then(response => {
            this.$modal.msgSuccess("审核通过");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    fetchUserList() {
      // 查询用户信息
      listUser().then(response => {
        this.userList = response.rows;
      });
    },
    fetchJobList() {
      // 查询数据同步任务
      listJob().then(response => {
        this.jobList = response.rows;
      });
    }
  }
};
</script>
